import os.path
pj = os.path.join

base_name = '_osg2.main'
org = file('input.cpp')
lines = org.readlines()
header_lines = []
code_lines = []
main_lines_before = []
main_lines_after = []
main_includes = []
main_func_calls = []
gen_dir = pj('src','osg_module','generated')

for l in lines:
   if l == '\n':
      pass
   elif l == '}\n':
      main_lines_after.append(l)
   elif l.endswith('register_free_functions();\n'):
      main_lines_after.append(l)
   elif l.endswith('_osg2_free_functions.pypp.hpp"\n'):
      main_includes.append(l)
   # Include mainheader in both main file and all helper files.
   elif l.endswith('osgmodule_mainheader.h"\n'):
      main_includes.append(l)
      header_lines.append(l)
   elif l.startswith('//'):
      header_lines.append(l)
   elif l.startswith('#include "boost'):
      header_lines.append(l)
      main_lines_before.append(l)
   elif l.startswith('#include'):
      header_lines.append(l)
   elif l.startswith('using namespace'):
      header_lines.append(l)
      main_lines_before.append(l)
   elif l.startswith('namespace bp'):
      header_lines.append(l)
      main_lines_before.append(l)
   elif l.startswith('BOOST_PYTHON_MODULE'):
      main_lines_before.append(l)
   else:
      code_lines.append(l)

print "Num total lines: %s Headers: %s Code: %s" % \
   (len(lines), len(header_lines), len(code_lines))

num_files = 15
lines_per_file = (len(code_lines)/num_files) + 1

start_line = 0
end_line = start_line + lines_per_file
file_num = 0

while start_line < len(code_lines):
   if end_line > len(code_lines):
      end_line = len(code_lines)

   file_name = base_name.replace('main', 'main%s'%file_num)
   cpp_location = pj(gen_dir, "%s.cpp"%file_name)
   hpp_file_name = "%s.hpp"%file_name
   hpp_location = pj(gen_dir, hpp_file_name)
   print "[%s] [%s][%s]" % (file_name, start_line, end_line)

   cpp_file = open(cpp_location, 'w+')
   cpp_file.writelines(header_lines)
   cpp_file.writelines('#include <osg_module/generated/%s>\n'%hpp_file_name)
   main_includes.append('#include <osg_module/generated/%s>\n'%hpp_file_name)
   cpp_file.write('#include <iostream>\n')
   cpp_file.write('void main%s_help(){\n' % file_num)
   main_func_calls.append('main%s_help();\n'%file_num)
   cpp_file.writelines(code_lines[start_line:end_line])
   #for l in code_lines[start_line:end_line]:
      #doc_l = l
      #doc_l = doc_l.replace('\n', '')
      #doc_l = doc_l.replace('\r', '')
      #doc_l = doc_l.replace('"', '')
      #doc_l = doc_l.strip()
      #cpp_file.write('std::cout << ": %s" << std::endl;\n'%(doc_l,))
      #cpp_file.write(l)
   cpp_file.write('}\n')
   hpp_file = open(hpp_location, 'w+')
   hpp_file.write('#ifndef _osg2_main_%s_\n' % file_num)
   hpp_file.write('#define _osg2_main_%s_\n' % file_num)
   hpp_file.write('void main%s_help();\n'%file_num)
   hpp_file.write('#endif //_osg2_main_%s_\n' % file_num)


   start_line = end_line
   end_line = start_line + lines_per_file
   file_num += 1


main_file_name = pj(gen_dir, '_osg2.main.cpp')
main_file = open(main_file_name, 'w+')
main_file.writelines(main_includes)
main_file.writelines(main_lines_before)
main_file.writelines(main_func_calls)
main_file.writelines(main_lines_after)


print "Lines/file:", lines_per_file

#for file_num in xrange(num_files):
#   print file_num
#   print lines/num_files
